import React, { Component, Fragment } from "react";
import MainLayout from "@component/mainLayout/mainLayout";
import { InterfaceG, channelCode, agreements_url } from "@config/Config";
import { Modal, Toast, List } from "antd-mobile";
import yhTools from "@tools/yh";

import Introduction from "@images/imageXHB/new-home/introduction.png";
//import CustomerService from '@images/imageXHB/new-home/customer-service.png';
//import SetUp from '@images/imageXHB/new-home/set-up.png';

import "./IntroductionBankLoans.scss";

class IntroductionBankLoans extends Component {
	constructor(props) {
		super(props);
		this.state = {
			objData: sessionStorage.getItem("walletHomeData")
				? JSON.parse(sessionStorage.getItem("walletHomeData"))
				: {
						nextProfit: "",
						nextProfitDay: "",
						rate: "0.00", // "0.00",//存款利率
						sumProfit: "0.00", //累积收益
						totalAmount: "0.00", //总金额
						totalCount: "", //总订单条数
						openStatus: "", //开户状态，0-已开户，1-未开户
						productFCode: "",
						productLabel: [], //产品标签
						productId: "",
						profitLabel: "",
						rateLabel: "",
						tradeNumber: "",
						electronicCardNo: "",
						balanceAmt: "0.00",
						productDetail: "A",
						saleStatus: 0
				  }
		};
	}
	componentDidMount() {
		this.getInitData();
	}
	getInitData() {
		let objs = {
			url: InterfaceG.holdingsInit,
			params: {
				channelCode: channelCode,
				productId: "1"
			},
			_alertShow: false
		};
		let _that = this;
		yhTools.request(objs).then(res => {
			if (res.code === "0") {
				sessionStorage.setItem("walletHomeData", JSON.stringify(res.data));
				_that.setState({
					objData: res.data,
					isRefresh: Math.random()
				});
			} else {
				_that.setState({
					loadStatus: "3" // （ '0'：数据加载中，'1'：加载完成，'2'：加载失败，'3'：系统正在升级）
				});
				Toast.info(res.message, 2);
			}
		});
	}
	/**
	 * @description: 点击头部设置图标
	 */
	showModal = () => () => {
		// e.preventDefault(); // 修复 Android 上点击穿透
		this.setState({
			showDialog: "moreAccountDialog  wrapper-out",
			popContent: "",
			modal1: true
		});
	};
	onClose = () => () => {
		this.setState({
			modal1: false
		});
	};
	//查看协议
	openContract(v) {
		if (v === 1) {
			this.props.history.push({
				pathname: "/iframeAgreements",
				state: {
					src: agreements_url + "/privacyAgreement.html",
					title: "永辉金融隐私政策"
				}
			}); //《用户隐私协议》
		} else if (v === 2) {
			this.props.history.push(`/serviceAgreementAuthorization`); //《客户服务协议》
		}
	}
	getText() {
		let obj = {
			0: "已停售",
			1: "确认转入",
			2: "今日已售罄，请明天早点来"
		};
		return obj[this.state.objData.saleStatus];
	}
	gotoNext(i) {
		let pageParams = {};
		if (i === 1) {
			//转出
			if (!this.formatMoney(this.state.objData.totalAmount)) {
				return;
			}
			// 判断已签约，并且有合并的订单才能进入一键转出界面
			if (
				this.state.objData.paySignStatus === "0" &&
				this.state.objData.mergeStatus == "0"
			) {
				//已开通
				pageParams = {
					url: `/transferXHBOutByPay/${this.state.objData.productId || ""}`
				};
			} else {
				//需要调用接口判断是否签约小辉宝支付
				pageParams = {
					url: `/depositDetail`
				};
			}
		} else if (i === 10) {
			if (!this.formatMoney(this.state.objData.totalAmount)) {
				return;
			}
			pageParams = {
				url: `/depositDetail`
			};
		} else if (i === 2) {
			//转入
			if (this.state.objData.saleStatus != "1") {
				Toast.info(this.getText(), 2);
				return;
			}
			pageParams = {
				url: `/transferXHBInto/${this.state.objData.productId || ""}`
			};
		} else if (i === 3) {
			pageParams = {
				url: `/walletInfo/${this.state.objData.productId || ""}`
			};
		} else if (i === 4) {
			this.appBtnMoney();
		} else if (i === 5) {
			pageParams = {
				url: `/cumulativeIncome`
			};
		} else if (i === 6) {
			pageParams = {
				url: `/xhbPorblem/${this.state.objData.productDetail}`
			};
		} else if (i === 7) {
			pageParams = {
				url: `/depositDetail`
			};
		} else if (i === 8) {
			pageParams = {
				url: `/bankDetailPage`
			};
		} else if (i === 9) {
			if (this.state.objData.paySignStatus === "2") {
				//开通中
				pageParams = {
					url: `/signRes`
				};
			} else if (this.state.objData.paySignStatus === "0") {
				//已开通
				pageParams = {
					url: `/payDetail`
				};
			} else {
				//未开通
				pageParams = {
					url: `/signXHB`
				};
			}
		}
		this.props.history.push({
			pathname: pageParams.url
		});
	}
	formatMoney(str) {
		return Number(str.replace(/,/g, "")) > 0;
	}
	/**转入转出限制 */
	queryXhbBankLockStatus(i, requrl) {
		if (i === 1 && !this.formatMoney(this.state.objData.totalAmount)) {
			//转出
			return;
		}
		let objs = {
			url: requrl,
			params: {
				productId: this.state.objData.productId
			},
			_alertShow: false
		};
		Toast.loading("加载中...", 15);
		yhTools
			.request(objs)
			.then(res => {
				Toast.hide();
				if (res.code === "0") {
					this.gotoNext(i);
				} else {
					Toast.info(res.message, 2);
				}
			})
			.then(() => {});
	}
	/**
	 * @description: 跳转交易明细
	 */
	gotoPopNext() {
		this.props.history.push({
			pathname: "/billRecord/${1}"
		});
	}
	//开始赚钱点击接口
	appBtnMoney() {
		let objs = {
			url: InterfaceG.xhbVerifyIdentity,
			params: {
				channelCode,
				version: "1.0"
			}
		};
		let _that = this;
		Toast.loading("加载中...", 100);
		yhTools.request(objs).then(res => {
			Toast.hide();
			if (res.code === "0") {
				const { pageNo, userName, idNo, mobilePhone, signDate } = res.data;
				if (pageNo === "01") {
					// 开户
					_that.props.history.push({
						pathname: `/uploadIDCardImgPage`
					});
				} else if (pageNo === "02") {
					// 身份核实
					_that.props.history.push({
						pathname: `/identityVerification`,
						state: { userName, idNo, mobilePhone, signDate }
					});
				} else {
					this.getInitData();
				}
			} else {
				Toast.info(res.message, 2);
			}
		});
	}
	render() {
		const headerParams = {
			header: {
				title: "小辉宝",
				// rightButt: <div className="header-right">
				//     <img src={CustomerService} onClick={this.onCustomer} />
				//     <img src={SetUp} onClick={this.showModal()} />
				// </div>,
				goToApp: this.state.flag
			},
			history: this.props.history,
			cacheLifecycles: this.props.cacheLifecycles,
			loadStatus: this.state.loadStatus // （ '0'：数据加载中，'1'：加载完成，'2'：加载失败）
		};
		const { modal1 } = this.state;
		const { openStatus, saleStatus } = this.state.objData;
		return (
			<MainLayout pageParams={headerParams}>
				<Modal
					popup
					className="wallet-home-modal"
					visible={modal1}
					onClose={this.onClose("modal1")}
					animationType="slide-up"
				>
					<List className="popup-list">
						<List.Item>
							<div className="item" onClick={this.gotoPopNext.bind(this)}>
								交易明细
							</div>
						</List.Item>
						<List.Item>
							<div className="cancel-btn" onClick={this.onClose("modal1")}>
								取消
							</div>
						</List.Item>
					</List>
				</Modal>
				<div className="introduction">
					<img src={Introduction} />
					<div className="btn-box">
						{openStatus == "1" && (
							<Fragment>
								<div className="contract_content">
									<p className="huise">点击按钮，表示您已阅读并同意</p>
									<p className="xieyi-p">
										<a
											className="xieyi"
											onClick={this.openContract.bind(this, 1)}
										>
											《永辉金融隐私政策》
										</a>
										<a
											className="xieyi"
											onClick={this.openContract.bind(this, 2)}
										>
											《永辉金融用户协议》
										</a>
									</p>
								</div>
								{saleStatus == "2" ? (
									<div className="out btn-common btn-disable">产品已售罄</div>
								) : (
									<div
										className="btn-common"
										onClick={this.gotoNext.bind(this, 4)}
									>
										立即存入
									</div>
								)}
							</Fragment>
						)}
						{openStatus == "0" && (
							<>
								{saleStatus == "2" ? (
									<div className="out btn-common btn-disable one-btn">
										产品已售罄
									</div>
								) : (
									<div
										className="btn-common one-btn"
										onClick={this.queryXhbBankLockStatus.bind(
											this,
											2,
											InterfaceG.queryXhbBuyBtnLockStatus
										)}
									>
										立即存入
									</div>
								)}
							</>
						)}
					</div>
				</div>
			</MainLayout>
		);
	}
}

export default IntroductionBankLoans;
